package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.orgarea.util;

import com.kingyun.gpsinspection.purificationservice.common.bean.ApplicationContextHelper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.OrgUser;
import com.kingyun.gpsinspection.purificationservice.facade.model.webservicesmodel.UasOrgUser;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.orgarea.JsmOrgUserService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/17.
 */
public class OrgUserPool implements Runnable {
    private String controlType;//1.添加，2.修改，3.删除
    private List<UasOrgUser> list;

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    public void setList(List<UasOrgUser> list) {
        this.list = list;
    }

    @Override
    public void run() {
        JsmOrgUserService orgUserService = (JsmOrgUserService) ApplicationContextHelper.getBean("orgUserService");
        if (orgUserService != null && StringUtils.isNotEmpty(controlType)) {
            List<OrgUser> paramList = new ArrayList<OrgUser>();
            try {
                orgUser(list, paramList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (paramList.size() > 0) {//组织机构用户关系信息处理
                if ("1".equals(controlType)) {
                    //添加
                    orgUserService.insertList(paramList);
                } else if ("2".equals(controlType)) {
                    //修改
                    orgUserService.updateList(paramList);
                } else if ("3".equals(controlType)) {
                    //删除
                    orgUserService.deleteList(paramList);
                }
            }
        }
    }

    /**
     * 组织机构用户关系处理
     *
     * @param list
     * @param paramList
     * @throws Exception
     */
    private void orgUser(List<UasOrgUser> list, List<OrgUser> paramList) throws Exception {
        if (list != null && list.size() > 0) {
            for (UasOrgUser uor : list) {
                OrgUser param = new OrgUser();
                BeanUtils.copyProperties(param, uor);
                paramList.add(param);
            }
        }
    }

}
